﻿Public Class ClsMMCollection
    'represents key based collection with special behaviour

#Region "Attributes"
#Region "Private attributes"
    Private MoCollection As Collection = New Collection
#End Region
#End Region
#Region "Properties"
#Region "Public attributes"
    Public ReadOnly Property NumberOfItems() As Integer
        Get
            Return MoCollection.Count
        End Get
    End Property
#End Region
#End Region
#Region "Functions"
    'adds item to collection
    Public Function AddItem(ByVal IoObject As Object, ByVal IsKey As String, Optional ByVal IbSilentMode As Boolean = False) As GeActionResult
        Try
            MoCollection.Add(IoObject, IsKey)
            Return GeActionResult.kkOk

        Catch ex As Exception
            If IbSilentMode Then
                Return GeActionResult.kkCancel
            Else
                LogEventFromException(ex, , , )
                Return GeActionResult.kkError
            End If
        End Try
    End Function
    'removes item from collection
    Public Function RemoveItem(ByVal IsKey As String, Optional ByVal IbSilentMode As Boolean = False) As GeActionResult
        Try
            MoCollection.Remove(IsKey)
            Return GeActionResult.kkOk

        Catch ex As Exception
            If IbSilentMode Then
                Return GeActionResult.kkCancel
            Else
                LogEventFromException(ex, , , )
                Return GeActionResult.kkError
            End If
        End Try
    End Function
    'changes key of item = remove it and add it again with new key
    Public Function ChangeKeyOfItem(ByVal IsOldKey As String, ByVal IsNewKey As String) As GeActionResult
        Try
            Dim LoObject As Object = GetItem(IsOldKey)
            RemoveItem(IsOldKey)
            AddItem(LoObject, IsNewKey)

            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'gets item from collection defined by key
    Public Function GetItem(ByVal IsKey As String, Optional ByVal IbSilentMode As Boolean = False) As Object
        Try
            Return MoCollection(IsKey)
        Catch ex As Exception
            If Not IbSilentMode Then LogEventFromException(ex, , , )
            Return Nothing
        End Try
    End Function
    'gets item from collection defined by key
    Public Function IsItemInCollection(ByVal IsKey As String) As Boolean
        Try
            Return IsInCollection(MoCollection, IsKey)
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return False
        End Try
    End Function
    'gets collection of items
    Public Function GetCollection() As Collection
        Return MoCollection
    End Function
    'gets number of items in collection
    Public Function Count() As Integer
        Return MoCollection.Count
    End Function
    'determines if collection is empty
    Public Function IsEmpty() As Boolean
        Return MoCollection.Count = 0
    End Function

#End Region
End Class
